package com.taihua_tech.basic.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;

public class DataBaseUtil {

	public static void InitDataBase() throws IOException {
/*		SqlSession session = MyBatisUtil.getSession();
		Properties pro = session.getConfiguration().getVariables();
		String driver = pro.getProperty("driver");
		String url = pro.getProperty("url");
		String username = pro.getProperty("name");
		String password = pro.getProperty("password");
		session.close();
		*/
		
/*		String conf = "db.properties";
		Properties pro = Resources.getResourceAsProperties(conf);
		String driver = pro.getProperty("datasource1.driver");
		String url = pro.getProperty("datasource1.url");
		String username = pro.getProperty("datasource1.name");
		String password = pro.getProperty("datasource1.password");*/
		
		String conf = "db.properties";
		Properties pro = Resources.getResourceAsProperties(conf);
		String driver = pro.getProperty("datasource2.driver");
		String url = pro.getProperty("datasource2.url");
		String username = pro.getProperty("datasource2.name");
		String password = pro.getProperty("datasource2.password");
		
		try {
			System.out.println(url + username + password);
			Class.forName(driver).newInstance();
			System.out.println(url + username + password);
			Connection conn = DriverManager.getConnection(url, username, password);
			ScriptRunner runner = new ScriptRunner(conn);
			
			Resources.setCharset(Charset.forName("GBK")); // 设置字符集,不然中文乱码插入错误
			runner.setLogWriter(null);// 设置是否输出日志

			runner.runScript(Resources.getResourceAsReader("org/tedu/util/resources/sql/DEMO.sql"));
			runner.closeConnection();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 配置文件路径及导出方法的调用
	public static void main(String[] args) throws Exception {
		  
	}
}
